﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using park.Common;

namespace park.Equipment
{
    public partial class MeteredAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPayStrategy();
            }
        }

        private void LoadPayStrategy()
        {
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            
             string strJson = sl.ListPayStrategy();
            //string strJson = "{ListPayStrategy:[{id:1,Strategy:\"收费策略1\"},{id:2,Strategy:\"收费策略2\"}]}";

            JObject obj = JObject.Parse(strJson);

            for (int i = 0; i < obj["ListPayStrategy"].Count(); i++)
            {
                ListItem li = new ListItem(obj["ListPayStrategy"][i]["Strategy"].ToString(), obj["ListPayStrategy"][i]["id"].ToString());
                this.ddlPayStrategy.Items.Add(li);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int cityId = (Utils.IsInt(Request.Form["ddl_City"]) ? int.Parse(Request.Form["ddl_City"]) : 0);
            int areaId = (Utils.IsInt(Request.Form["ddl_Area"]) ? int.Parse(Request.Form["ddl_Area"]) : 0);
            int streetId = (Utils.IsInt(Request.Form["ddl_Street"]) ? int.Parse(Request.Form["ddl_Street"]) : 0);
            string meteredNo = this.txtMeteredNo.Text;
            string meteredCode = this.txtMeteredCode.Text;
            string meteredName = this.txtMeteredName.Text;
            string longitude = this.txtLongitude.Text;
            string latitude = this.txtLatitude.Text;
            string inducedScreenNo = this.txtInducedScreen.Text;
            string connection = this.txtConnection.Text;
            string pricingStrategy = this.ddlPayStrategy.SelectedValue;
            DateTime installTime = string.IsNullOrEmpty(this.txtInstallTime.Text) ? DateTime.Parse("1900-01-01") : DateTime.Parse(this.txtInstallTime.Text);

            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            bool flag = sl.SaveMetered(cityId, areaId, streetId, meteredNo, meteredCode, meteredName, longitude, latitude, inducedScreenNo, connection, pricingStrategy, installTime);

            string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

            Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");
        }
    }
}